package cn.myfirefly.moming.house.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.myfirefly.moming.house.dao.mapper.SpfTradeWageMapper;
import cn.myfirefly.moming.house.entity.SpfTradeWage;
import cn.myfirefly.moming.house.model.SpfTradeWageDto;
import cn.myfirefly.moming.house.model.SpfTradeWageFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.FilterUtil;

@Repository
public class SpfTradeWageDao extends BaseDao<SpfTradeWage> {
	
	@Autowired
	private SpfTradeWageMapper spfTradeWageMapper;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageResultDto<SpfTradeWage> getList(SpfTradeWageFilterDto dto) {
		Session session = getCurrentSession();
		PageResultDto<SpfTradeWage> resultDto = new PageResultDto<>();
		
		StringBuffer countSql = new StringBuffer("select count(r) from SpfTradeWage r where 1=1 ");
		StringBuffer querySql = new StringBuffer("from SpfTradeWage r where 1=1 ");
		
		List paramList = new ArrayList();
		FilterUtil.handleFilterSql(dto, "r", countSql, querySql, paramList);
		
		Query query = session.createQuery(countSql.toString());
		FilterUtil.setQueryParams(query, paramList);
		Number number = (Number)query.uniqueResult();
		resultDto.setTotal(number.intValue());
		if (number.intValue() > 0) {
			query = session.createQuery(querySql.toString());
			FilterUtil.setQueryParams(query, paramList);
			List<SpfTradeWage> list = query
					.setFirstResult((dto.getPageCurrent() - 1) * dto.getPageSize())
					.setMaxResults(dto.getPageSize())
					.list();
			resultDto.setPageCurrent(dto.getPageCurrent());
			resultDto.setList(list);
		}
		return resultDto;
	}
	
	public PageResultDto<SpfTradeWageDto> getList(SpfTradeWageDto dto) {
		PageResultDto<SpfTradeWageDto> resultDto = new PageResultDto<>();
		//参数处理
		Integer tradeContractId = null;
		if (dto.getTradeContractId() != null) {
			tradeContractId = dto.getTradeContractId();
		}
		//分页处理
		Page<SpfTradeWageDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<SpfTradeWageDto> list = spfTradeWageMapper.getListBy(tradeContractId);
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
}
